package com.useeinfo.oa.modules.inspection.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrderLog;
import com.useeinfo.oa.modules.inspection.enums.InsOrderOperationEnum;
import com.useeinfo.oa.modules.maintaining.enums.OrderOperationEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("inspectionOrderLogDao")
public class InspectionOrderLogDao implements CrudDao<InspectionOrderLog> {
    private final static Logger logger = LoggerFactory.getLogger(InspectionOrderLogDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String progress = queryHash.get("progress");
        if (!StringUtils.isEmpty(progress)) {
//            List<InsOrderOperationEnum> userList = new ArrayList<>();
//            for(String pro:progress.split(",")){
//                userList.add(InsOrderOperationEnum.valueOf(pro));
//            }
            conditionHash.put("s.orderOperationEnum in "+progress+" and 1= ?{paramIndex}", 1);
        }
        String orderOperationEnum = queryHash.get("orderOperationEnum");
        if (!StringUtils.isEmpty(orderOperationEnum)) {
            conditionHash.put("(s.orderOperationEnum = ?{paramIndex}) ", InsOrderOperationEnum.valueOf(orderOperationEnum));
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(s.enterprise.id = ?{paramIndex}) ", StringConverters.null2Long(enterpriseId));
        }
        String inspectionOrderId = queryHash.get("inspectionOrderId");
        if (!StringUtils.isEmpty(inspectionOrderId)) {
            conditionHash.put("(s.inspectionOrder.id = ?{paramIndex}) ", StringConverters.null2Long(inspectionOrderId));
        }
        String name = queryHash.get("name");
        if (!StringUtils.isEmpty(name)) {
            conditionHash.put("(s.inspectionOrder.orderName like ?{paramIndex}) ", "%"+name+"%");
        }
        String detail = queryHash.get("detail");
        if (!StringUtils.isEmpty(detail)) {
            conditionHash.put("(s.detail like ?{paramIndex}) ","%"+detail+"%" );
        }
        String startTime = queryHash.get("startTime");
        if (!StringUtils.isEmpty(startTime)) {
            conditionHash.put("(s.createDate >= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",startTime));
        }
        String endTime = queryHash.get("endTime");
        if (!StringUtils.isEmpty(endTime)) {
            conditionHash.put("(s.createDate <= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",endTime));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from InspectionOrderLog s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<InspectionOrderLog> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from InspectionOrderLog s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<InspectionOrderLog> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, InspectionOrderLog.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public InspectionOrderLog findModel(Long id) {
        return entityManager.find(InspectionOrderLog.class, id);
    }

    @Override
    public Integer add(InspectionOrderLog model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("InspectionOrderLogDaoImpl添加inspectionOrderLog成功！");
        return 1;
    }

    @Override
    public Integer update(InspectionOrderLog model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        InspectionOrderLog inspectionOrderLog = entityManager.find(InspectionOrderLog.class, id);
        entityManager.remove(inspectionOrderLog);
        return 1;
    }

    @Override
    public Integer deleteLogic(InspectionOrderLog model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
